﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;

namespace OpenPanekitEditor
{
    public partial class MainForm : Form
    {
        private int childFormNumber = 0;
        //public OldDocument doc;

        public MainForm()
        {
            TextConsole.WriteLine("OpenPanekit Ver. 0.01 alpha");
            TextConsole.WriteLine("copyright ");

            FormClosed += new FormClosedEventHandler(MainForm_FormClosed);
            InitializeComponent();
            Document.LoadSettings();
            Document.LoadPanelPlugin();
        }

        void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            Document.SaveSettings();
        }
        private void createNewModel_Click(object sender, EventArgs e)
        {
            Document.Model model = Document.CreateNewModel();
            model.View = new Forms.ModelView(model);
            // 表示する前に、この MDI フォームの子に設定します。
            model.View.MdiParent = this;
            model.View.Text = model.FileBody;
            model.View.Show();
        }
        private void createNewScene_Click(object sender, EventArgs e)
        {
            Document.Scene scene = Document.CreateNewScene();
            scene.View = new Forms.SceneView(scene);
            // 表示する前に、この MDI フォームの子に設定します。
            scene.View.MdiParent = this;
            scene.View.Text = "Scene " + childFormNumber++;
            scene.View.Show();
        }
        private void terminateApp_Click(object sender, EventArgs e)
        {
            foreach (Form childForm in MdiChildren)
            {
                childForm.Close();
            }
            this.Close();
        }
        private void openScene_Click(object sender, EventArgs e)
        {
            MessageBox.Show(Resouces.Strings.Static_Underconstruction);
        }
        private void createDebugLog_Click(object sender, EventArgs e)
        {
            Forms.ConsoleForm child = new Forms.ConsoleForm();
            // 表示する前に、この MDI フォームの子に設定します。
            child.MdiParent = this;
            child.Text = "DebugLog " + childFormNumber++;
            child.Show();
        }
        private void settingKeyAssign_Click(object sender, EventArgs e)
        {
            Forms.KeySettingForm child = new Forms.KeySettingForm();
            child.MdiParent = this;
            child.Show();
        }
        private void openModel_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            fd.Filter = Resouces.Strings.ModelFileDialogString;
            if (fd.ShowDialog() == DialogResult.OK)
            {
                Document.Model model = Document.LoadXMLModel(fd.FileName);
                if (model != null)
                {
                    try
                    {
                        model.View = new Forms.ModelView(model);
                        model.View.MdiParent = this;
                        model.View.Text = model.FileBody;
                        model.View.Show();
                    }
                    catch (Exception exp)
                    {
                        System.Windows.Forms.MessageBox.Show(Resouces.Strings.MainForm_LoadError);
                    }
                }
            }
        }

        private void options_Click(object sender, EventArgs e)
        {
            Forms.SettingForm sform = new Forms.SettingForm();
            sform.PluginFolder = Document.Settings.PluginFolder;
            sform.PhysicsUpdateRate = Document.Settings.PhysicsUpdateRate;
            if (sform.ShowDialog() == DialogResult.OK)
            {
                Document.Settings.PluginFolder = sform.PluginFolder;
                Document.Settings.PhysicsUpdateRate = sform.PhysicsUpdateRate;
            }
        }

    }
}
